package com.shaoshaohuo.app.ui.ec;

import java.util.List;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.Browser;
import com.shaoshaohuo.app.entity.BrowserEntity;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.ui.BaseActivity;
import com.shaoshaohuo.app.adapter.BrowserRecordAdapter;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.view.XListView;
import com.shaoshaohuo.app.view.XListView.IXListViewListener;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * 我的页面 中 浏览记录 页面
 * 
 * @author cao
 *
 */
public class EcBrowseRecordActivity extends BaseActivity implements IXListViewListener {

	private TopbarView mTopBar;
	private XListView mListView;
	private int size = 10;
	private List<Browser> mDataList;
	private BrowserRecordAdapter mAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_browse_record);
		initView();
		initData();
		requestData(true);
	}

	private void initView() {
		mTopBar = (TopbarView) findViewById(R.id.topbar);
		mListView = (XListView) findViewById(R.id.listview);
	}

	private void initData() {
		mTopBar.setCenterText("浏览记录");
		mTopBar.setLeftView(true, true);

		mListView.setPullLoadEnable(true);
		mListView.setPullRefreshEnable(true);
		mListView.setXListViewListener(this);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO 切换到商品页
			}
		});

	}

	private void requestData(boolean b) {
		if (b) {
			startLoadingDialog();
			size = 10;
		}else{
			size += 10;
		}

		RequestService.getInstance().browseRecord(this, size + "", BrowserEntity.class, new RequestListener() {

			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				dismissLoadingDialog();
				if (resultData.isOk()) {
					BrowserEntity entity = (BrowserEntity) resultData;
					mDataList = entity.getData().getList();
					if (mDataList != null) {
						if (mDataList.size() < size) {
							mListView.endLoadingMore("没有更多了");
						}
						setAdapter(mDataList);
					} else {
						mListView.endLoadingMore("没有更多了");
					}
				} else {
					showToast(resultData.getMsg());
				}
				onLoad();
			}

			@Override
			public void onFailed(int requestCode, Exception error, String msg) {
				dismissLoadingDialog();
				showToast(msg);
				onLoad();
			}
		});
	}

	protected void setAdapter(List<Browser> mDataList2) {
		if (mAdapter == null) {
			mAdapter = new BrowserRecordAdapter(this, mDataList2, false);
			mListView.setAdapter(mAdapter);
		} else {
			mAdapter.setData(mDataList2);
		}
	}

	@Override
	public void onRefresh() {
		requestData(true);
	}

	@Override
	public void onLoadMore() {
		requestData(false);
	}

	private void onLoad() {
		mListView.stopRefresh();
		mListView.stopLoadMore();
	}

}
